﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebAutoUtils;

namespace WebAutoUtils
{
    public class TestDataExceptionRules : ITestDataExceptionRules
    {
        public enum ExceptionRuleName
        {
            DatabaseConnectionString=0,
            TestRepoLocation=1,
            TestResultsLocation=2,
            TestStartURL=3,
            TestEndOrFailURL=4,
            TestIterations=5,
            TestExcelFile=6

        }


        public struct DataExceptionRules
        {
            public ExceptionRuleName RuleName { get; set; }
            public string NotAllowedValue { get; set; }
            public string AllowedValue { get; set; }

        }


        public List<DataExceptionRules> ListOfExceptionRules { get; set; }

        public bool IsExcelFileAllowed(string excelFile)
        {
            bool returnValue = true;
            try
            {
                if (!string.IsNullOrEmpty(excelFile))
                {
                    if (ListOfExceptionRules != null)
                    {
                        var ruleNotAllowedValue =
                            ListOfExceptionRules.FirstOrDefault(
                                i => i.RuleName == ExceptionRuleName.TestEndOrFailURL).NotAllowedValue;
                        if (ruleNotAllowedValue != null)
                        {
                            if (ruleNotAllowedValue.Contains(","))
                            {
                                List<string> ruleNotAllowedValueArray = ruleNotAllowedValue.Split(',').ToList();
                                foreach (var item in ruleNotAllowedValueArray)
                                {
                                    if (item.Trim().ToLower() == excelFile.Trim().ToLower())
                                    {
                                        returnValue = false;
                                        break;
                                    }

                                }
                            }
                            else
                            {
                                if (ruleNotAllowedValue.Trim().ToLower() == excelFile.Trim().ToLower())
                                {
                                    returnValue = false;
                                }
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Exception rules failure in IsEndOrFailURLAllowed." + ex.ToString());

            }
            return returnValue;

        }

        public bool IsEndOrFailURLAllowed(string testFailURL)
        {
            bool returnValue = true;
            try
            {
                if (!string.IsNullOrEmpty(testFailURL))
                {
                    if (ListOfExceptionRules != null)
                    {
                        var ruleNotAllowedValue =
                            ListOfExceptionRules.FirstOrDefault(
                                i => i.RuleName == ExceptionRuleName.TestEndOrFailURL).NotAllowedValue;
                        if (ruleNotAllowedValue != null)
                        {
                            if (ruleNotAllowedValue.Contains(","))
                            {
                                List<string> ruleNotAllowedValueArray = ruleNotAllowedValue.Split(',').ToList();
                                foreach (var item in ruleNotAllowedValueArray)
                                {
                                    string compareString1 = item.Trim().Replace(" ", "").Replace("-", "").ToLower();
                                    string compareString2 = testFailURL.Trim().Replace(" ", "").Replace("-", "").ToLower();
                                    if (compareString1 == compareString2)
                                    {
                                        returnValue = false;
                                        break;
                                    }

                                }
                            }
                            else
                            {
                                if (ruleNotAllowedValue.Trim().ToLower() == testFailURL.Trim().ToLower())
                                {
                                    returnValue = false;
                                }
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Exception rules failure in IsEndOrFailURLAllowed." + ex.ToString());

            }
            return returnValue;

        }

        public bool IsStartURLAllowed(string testStartURL)
        {
            bool returnValue = true;
            try
            {
                if (!string.IsNullOrEmpty(testStartURL))
                {
                    if (ListOfExceptionRules != null)
                    {
                        var ruleNotAllowedValue =
                            ListOfExceptionRules.FirstOrDefault(
                                i => i.RuleName == ExceptionRuleName.TestStartURL).NotAllowedValue;
                        if (ruleNotAllowedValue != null)
                        {
                            if (ruleNotAllowedValue.Contains(","))
                            {
                                List<string> ruleNotAllowedValueArray = ruleNotAllowedValue.Split(',').ToList();
                                foreach (var item in ruleNotAllowedValueArray)
                                {
                                    string compareString1 = item.Trim().Replace(" ", "").Replace("-", "").ToLower();
                                    string compareString2 = testStartURL.Trim().Replace(" ", "").Replace("-", "").ToLower();
                                    if (compareString1 == compareString2)
                                    {
                                        returnValue = false;
                                        break;
                                    }

                                }
                            }
                            else
                            {
                                if (ruleNotAllowedValue.Trim().ToLower() == testStartURL.Trim().ToLower())
                                {
                                    returnValue = false;
                                }
                            }

                        }
                    }
                }
            }catch(Exception ex)
            {
                throw new Exception("Exception rules failure in IsStartURLAllowed." + ex.ToString());

            }
            return returnValue;

        }

        public bool IsDataConnectionAllowed(string testDataConnectionString)
        {
            bool returnValue = true;
            try
            {
                if (!string.IsNullOrEmpty(testDataConnectionString))
                {
                    if (ListOfExceptionRules != null)
                    {
                        var ruleNotAllowedValue =
                            ListOfExceptionRules.FirstOrDefault(
                                i => i.RuleName == ExceptionRuleName.DatabaseConnectionString).NotAllowedValue;
                        if (ruleNotAllowedValue != null)
                        {
                            if (ruleNotAllowedValue.Contains(","))
                            {
                                List<string> ruleNotAllowedValueArray = ruleNotAllowedValue.Split(',').ToList();
                                foreach (var item in ruleNotAllowedValueArray)
                                {
                                    string compareString1 = item.Trim().Replace(" ", "").Replace("-","").ToLower();
                                    string compareString2 = testDataConnectionString.Trim().Replace(" ", "").Replace("-", "").ToLower();
                                    if (compareString1 == compareString2)
                                    {
                                        returnValue = false;
                                        break;
                                    }

                                }
                            }
                            else
                            {
                                if (ruleNotAllowedValue.Trim().ToLower() == testDataConnectionString.Trim().ToLower())
                                {
                                    returnValue = false;
                                }
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Exception rules failure in IsDataConnectionValid." + ex.ToString());

            }
            return returnValue;

        }
    }
}
